package FreebaseMatchWiki;

import java.io.*;
import zclTool.*;
import java.util.*;

public class PropNotMatchWpidofThatType {
	public static void main(String []args) throws Exception{
		String tsvfilelist = args[0];
		String dir = args[1];
		String wpid_guid_file = args[2];
		
		HashSet<String>guid_wpid = new HashSet<String>();
		DelimitedReader br_wpid_guid = new DelimitedReader(wpid_guid_file);
		String []lines;
		
		while((lines = br_wpid_guid.read())!=null){
			String guid = lines[0];
			//int wpid = Integer.parseInt(lines[1]);
			guid_wpid.add(guid);
		}
		
		BufferedReader br_files = new BufferedReader(new FileReader(tsvfilelist));
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(args[3]),"utf-8");
		int filen = 0;
		String line="";
		while((line = br_files.readLine())!=null){
			filen++;
			System.out.println("filen "+filen+"\t"+line);
			String file = line.replace("./", dir+"/");
			String filetype = line.replace("./", "").replace(".tsv", "");
			BufferedReader br = new BufferedReader(new FileReader(file));
			String line2 = br.readLine();//this is metadata
			String []metadata = line2.split("\t");
			int []appear = new int[metadata.length];
			int totalInts = 0;
			while((line2 = br.readLine())!=null){
				String []abc = line2.split("\t");
				String guid = abc[1].replace("/guid/", "");
				if(guid_wpid.contains(guid)==false)continue;
				else{
					totalInts++;
					for(int i=2;i<metadata.length && i< abc.length;i++){
						String prop = metadata[i];
						String value = abc[i];
						if(value.length()>=1){
							appear[i]++;
							//osw.write(guid+"\t"+filetype+"\t"+prop+"\t"+value+"\n");
						}
					}
				}
			}
			for(int i=2;i<metadata.length;i++){
				if(totalInts>0)
					osw.write(filetype+"@"+metadata[i]+"\t"+appear[i]+"\t"+totalInts+"\n");
			}
			//check all metadata;
//			for(int i=0;i<metadata.length; i++){
//				osw.write(metadata[i]+"\t");
//			}
			
			//osw.write("\n");
//			while((line2 = br.readLine())!=null){
//				
//			}
			br.close();
			
		}
		osw.close();
		br_files.close();
	}
	

}
